import { Debtor } from "../../../../../../typings/types/debtor"
import { Operation } from "../../../../../../typings/types/operation"
import { getChattelMortgageDetailAPI, getLandAuctionDetailAPI, getLandMortgageDetailAPI } from "../../../api/operatingRisk"
import { formateDigit } from "../../../../../utils/util"
import { LitigationVM } from "../../../litigation/litigationVM"

export namespace OperationRiskVM {

  export enum Scene {
    none = '',
    landAuction = 'landAuction',
    chattelMortgage = 'chattelMortgage',
    landMortgage = 'landMortgage'
  }

  export interface Cell {
    rows: Debtor.BaseInfoList[]
    title: string
  }
}


export class OperationRiskDetailViewModel implements LitigationVM.ViewModelInterface<number, OperationRiskVM.Cell[]> {
  constructor() {
  }
  async loadDataForScene(scene: OperationRiskVM.Scene, id: number, successCallback: (res: OperationRiskVM.Cell[]) => void, finallyCallback: (() => void) = () => { }) {
    wx.showLoading({
      title: ''
    })
    try {
      let tmp: OperationRiskVM.Cell[] = []
      switch (scene) {
        case OperationRiskVM.Scene.chattelMortgage:
          const res = await getChattelMortgageDetailAPI(id)
          if (res.content) {
            console.log(res.content)
            tmp = this.processChattelMortgageListDatas(res.content)
          }
          break
        case OperationRiskVM.Scene.landAuction:
          const res_c = await getLandAuctionDetailAPI(id)
          if (res_c.content) {
            console.log(res_c.content)
            tmp = this.processLandAuctionListDatas(res_c.content)
          }
          break
        case OperationRiskVM.Scene.landMortgage:
          const res_r = await getLandMortgageDetailAPI(id)
          if (res_r.content) {
            console.log(res_r.content)
            tmp = this.processLandMortgageListDatas(res_r.content)
          }

          break

        default: break

      }
      if (tmp.length > 0) {
        successCallback(tmp)
      }

    } finally {
      wx.hideLoading()
      finallyCallback()
    }

  }

  processChattelMortgageListDatas(content: Operation.ChattelMortgageDetail) {

    let tmp: OperationRiskVM.Cell[] = []
    if (content.operationMpledgeReq) {
      const status = content.operationMpledgeReq.status
      let cell: OperationRiskVM.Cell = {
        title: '动产抵押登记信息',
        rows: [
          { title: '登记机关', val: content.operationMpledgeReq.registerOffice, isShowLine: false },
          { title: '登记编号', val: content.operationMpledgeReq.registerNo, isShowLine: false },
          { title: '数额', val: content.operationMpledgeReq.debtSecuredAmount, sectitle: '状态', secval: status, secstyle: status === '有效' ? 'color:#00AD65' : 'color:#F04040' },
          { title: '登记时间', val: content.operationMpledgeReq.registerDate },
        ]
      }
      tmp.push(cell)
    }

    if (content.pledgeeReq) {
      let cell: OperationRiskVM.Cell = {
        title: '抵押权人信息',
        rows: [
          { title: '抵押权人名称', val: content.pledgeeReq.name, isShowLine: false },
          { title: '企业名称', val: content.pledgeeReq.companyName, sectitle: '抵押权人证照类型', secval: content.pledgeeReq.identityType },
          { title: '证件号码', val: content.pledgeeReq.identityNo },
        ]
      }
      tmp.push(cell)
    }

    if (content.guaranteedRightReq) {
      let cell: OperationRiskVM.Cell = {
        title: '被担保主债权信息',
        rows: [
          { title: '企业名称', val: content.guaranteedRightReq.companyName, isShowLine: false },
          { title: '金额', val: formateDigit(content.guaranteedRightReq.amount), sectitle: '种类', secval: content.guaranteedRightReq.kind, style: 'color:#F04040', valsubfix: '万元' },
          { title: '担保范围', val: content.guaranteedRightReq.assuranceScope },
          { title: '履行义务', val: content.guaranteedRightReq.fulfillObligation },
          // { title: '备注', val: content.guaranteedRightReq.remark },
        ]
      }
      tmp.push(cell)
    }

    if (content.guaranteeReq) {
      let cell: OperationRiskVM.Cell = {
        title: '抵押物信息',
        rows: [
          { title: '抵押物名称', val: content.guaranteeReq.name, isShowLine: false },
          { title: '所有权或使用权归属', val: content.guaranteeReq.ownership },
          { title: '数量、质量、状况、所在地等情况', val: content.guaranteeReq.other },
          // { title: '备注', val: content.guaranteeReq.remark },
        ]
      }
      tmp.push(cell)
    }

    if (content.cancelReq) {
      let cell: OperationRiskVM.Cell = {
        title: '取消信息',
        rows: [
          { title: '种类', val: content.cancelReq.cancelReason, sectitle: '取消时间', secval: content.cancelReq.cancelDate },
        ]
      }
      tmp.push(cell)
    }
    return tmp
  }

  processLandAuctionListDatas(content: Operation.LandAuctionDetail) {

    let tmp: OperationRiskVM.Cell[] = []
    if (content.landaucParty) {
      let cell: OperationRiskVM.Cell = {
        title: '土地拍卖使用权人',
        rows: [
          { title: '企业名称', val: content.landaucParty.companyName, isShowLine: true },
          { title: '变更时间', val: content.landaucParty.dates, sectitle: '更新时间', secval: content.landaucParty.updatedDate },
        ]
      }
      tmp.push(cell)
    }

    if (content.landaucResult) {
      let cell: OperationRiskVM.Cell = {
        title: '土地拍卖结果',
        rows: [
          { title: '项目位置', val: content.landaucResult.projectLocation, isShowLine: false },
          { title: '项目名称', val: content.landaucResult.projectName, isShowLine: true },
          { title: '行政区', val: content.landaucResult.regArea, sectitle: '电子监管号', secval: content.landaucResult.elecSuperNumber },
          { title: '土地总面积', val: content.landaucResult.landArea, sectitle: '供地方式', secval: content.landaucResult.landSupplyType, style: 'color:#F04040', valsubfix: ' 公顷' },
          { title: '土地使用年限', val: content.landaucResult.landUseTerm, isShowLine: true },
          { title: '行业分类', val: content.landaucResult.industry, sectitle: '土地等级', secval: content.landaucResult.landGrade },
          { title: '成交价格', val: formateDigit(content.landaucResult.turnoverPrice), sectitle: '批准单位', secval: content.landaucResult.approvalOrg, style: 'color:#F04040', valsubfix: '万元' },
          { title: '土地使用权人', val: content.landaucResult.landUser, isShowLine: true },
          { title: '约定交地时间', val: content.landaucResult.agreeDeliveryDate, sectitle: '约定开工时间', secval: content.landaucResult.agreeStartDate },
          { title: '约定竣工时间', val: content.landaucResult.agreeCompletionDate, sectitle: '实际竣工时间', secval: content.landaucResult.actualCompletionDate },
          { title: '合同签订时间', val: content.landaucResult.signDate, sectitle: '变更时间', secval: content.landaucResult.dates },
          { title: '更新时间', val: content.landaucResult.updateDate, sectitle: '', secval: '' },
        ]
      }
      tmp.push(cell)
    }

    if (content.agreedVolume) {
      let cell: OperationRiskVM.Cell = {
        title: '约定容积率',
        rows: [
          { title: '容积率上限', val: content.agreedVolume.maxFloorAreaRatio, sectitle: '容积率下限', secval: content.agreedVolume.minFloorAreaRatio },
        ]
      }
      tmp.push(cell)
    }

    return tmp
  }

  processLandMortgageListDatas(content: Operation.LandMortgageList) {

    let tmp: Debtor.BaseInfoList[] = [

      { title: '土地使用权证号', val: content.usufructNo, isShowLine: true },
      { title: '土地抵押人名称', val: content.mortgagorName, isShowLine: false },
      { title: '土地抵押权人名称', val: content.mortgageName, isShowLine: false },
      { title: '抵押土地用途', val: content.mortgagePurpose, isShowLine: true },
      { title: '抵押金额', val: formateDigit(content.mortgagePrice), sectitle: '评估金额', secval: formateDigit(content.assessmentPrice), style: 'color:#F04040', secstyle: 'color:#F04040', valsubfix: '万元', secvalsubfix: '万元' },
      { title: '土地面积', val: content.acreage, sectitle: '抵押面积', secval: content.mortgageAcreage, style: 'color:#F04040', secstyle: 'color:#F04040', valsubfix: '公顷', secvalsubfix: '公顷' },
      { title: '宗地编号', val: content.landNo, sectitle: '土地他项权利人证号', secval: content.obligeeNo, isShowLine: true },
      { title: '抵押土地权属性质与使用权类型', val: content.natureAndType, isShowLine: false },
      { title: '宗地标识', val: content.landSign, isShowLine: true },
      { title: '土地抵押登记起始时间', val: content.onBoardStartTime, sectitle: '土地抵押结束时间', secval: content.onBoardEndTime, isShowLine: false }
    ]

    return [{
      title: '',
      rows: tmp
    }]
  }

}